:- import "dumper.sl" #
:- import "emitter.sl" #
:- import "instruction.sl" #
:- import "operands.sl" #
:- import "platform.sl" #
:- import "tables.sl" #
:- import "%%/lib/dynamic.sl" #
:- import "%%/lib/expression.sl" #
:- import "%%/lib/list.sl" #
:- import "%%/lib/scanner.sl" #


-- Basic Flow --

Assemble .filename :-
	scanner.new .scanner,
	scanner.twoCharSymbols .scanner (">>", "<<", "- ", "--", "-=", "=-",),
	scanner.lineCommenter .scanner (SYM "--"),
	scanner.openCommenter .scanner (SYM "-="),
	scanner.closeCommenter .scanner (SYM "=-"),
	,
	parser.new .parser,
	parser.scanner .parser .scanner,
	parser.operators .parser (
		ASGop (SYM "=") LEFT,
		ADDop (SYM "+") LEFT,
		SUBop (SYM "- ") RIGHT,
		MULop (SYM "*") LEFT,
		DIVop (SYM "/") RIGHT,
		NEGop (SYM "-") PREFIX,
		(SYM "(") (SYM ")") PAREN,
	),
	parser.nodeParser .parser Parse.ExpressionNode,
	set.pointer PARSER .parser,
	,
	set PASS 1,
	Assemble.Pass .parser .filename -- Grab label addresses in first pass
	,
	set PASS 2,
	stream.stdout OUT.BIN.FILE,
--	Emit.WriteTo OUT.BIN.FILE,
	Assemble.Pass .parser .filename,
	Emit.Complete,
	stream.close OUT.BIN.FILE
	,
	unset PARSER,
	parser.dispose .parser,
	scanner.dispose .scanner ! #

Assemble.Pass .parser .filename :-
	parser.scanner .parser .scanner,
	stream.readFile IN.ASM.FILE .filename,
	scan.readFrom .scanner IN.ASM.FILE,
	Emit.Reset,
	Process.Instructions .parser,
	stream.close IN.ASM.FILE ! #

Error .message :-
	get PASS 1,
	Error.Message .message #
Error . #

Error.PassTwo .message :-
	get PASS 2,
	Error.Message .message #
Error.PassTwo . #

Error.Message .message :-
	scanner.currentLine (parser.scanner (get PARSER ^) ^) .line,
	write "LINE" .line "ERROR:" .message, nl ! #
